<template>
  <vol-box
    :lazy="true"
    v-model="model"
    title="未采购商品明细"
    :height="400"
    :width="1200"
    :padding="5"
    :onModelClose="onModelClose"
  >
    <purchaseApplicationNotMx
      ref="purchaseApplicationNotMx"
    ></purchaseApplicationNotMx>
    <template #footer>
      <div>
        <el-button type="primary" size="small" @click="confirmBtn"
          >确认</el-button
        >
      </div>
    </template>
  </vol-box>
</template>
    
    <script>
import VolBox from "@/components/basic/VolBox.vue";
import purchaseApplicationNotMx from "@/views/purchaseinfo/purchase_application_not_mx/q_purchase_application_not_mx.vue";
import { ElMessage } from "element-plus";

export default {
  components: {
    "vol-box": VolBox,
    purchaseApplicationNotMx,
  },
  data() {
    return {
      model: false,
    };
  },
  methods: {
    async onModelClose() {
      this.$refs.purchaseApplicationNotMx.searchFormFields.采购申请单号 = null;
      this.$refs.purchaseApplicationNotMx.searchFormFields.申请人 = null;
      this.$refs.purchaseApplicationNotMx.searchFormFields.编码 = null;
      this.$refs.purchaseApplicationNotMx.searchFormFields.名称 = null;
    },
    async open() {
      let setIntervalTiem = setInterval(() => {
        if (this.$refs.purchaseApplicationNotMx) {
          clearInterval(setIntervalTiem);
          this.$refs.purchaseApplicationNotMx.$refs.grid.search();
        }
      }, 100);
      this.model = true;
    },
    async confirmBtn() {
      let rowObj =
        this.$refs.purchaseApplicationNotMx.$refs.grid.getSelectRows();
      if (!rowObj.length) return this.base.ElMessageFn("请至少选择一个商品");
      let list = await rowObj.filter(
        (item) => item.审核状态 == 1 && item.采购状态 == 0
      ); //审核通过&&未采购
      let list2 = await rowObj.filter((item) => item.审核状态 != 1); //非审核通过
      let list3 = await rowObj.filter((item) => item.采购状态 != 0); //非未采购
      if (list2.length) {
        let str = "";
        list2.forEach((r) => {
          if (str == "") str += r.Goods_name;
          else str = str + "," + r.Goods_name;
        });
        this.$message.warning(`商品${str}未审核通过`);
      }
      if (list3.length) {
        let str = "";
        list3.forEach((r) => {
          if (str == "") str += r.Goods_name;
          else str = str + "," + r.Goods_name;
        });
        this.$message.warning(`商品${str}非未采购`);
      }
      this.$emit("parentCall", ($parent) => {
        try {
          let parentList = $parent.$refs.detail.rowData;
          let set = new Set(parentList.map((item) => item.Purchase_applicationID)); //得到由明细表goods_sub组成的数组
          let commonObjects = list.filter((item) => set.has(item.ID)); //得到明细表中已包含的选中行
          let goodsNamesSet = new Set(
            commonObjects.map((item) => item.ID)
          ); //相同商品的编码
          if (commonObjects.length) {
            list = list.filter(
              (item) => !goodsNamesSet.has(item.ID)
            ); //在选中数据中剔除明细表已包含的行数据
            let str = "";
            commonObjects.forEach((r) => {
              if (str == "") str += r.名称;
              else str = str + "," + r.名称;
            });
            this.$message.warning(`商品${str}已存在此采购单`);
          }
          let arr = list.map((item) => {
            return {
              Purchase_applicationID: item.ID,
              Voucher_sub: item.采购申请单号,
              // Goods_price:item.ID,
              Tax_rate: item.税率,
              Goods_num: item.采购数量,
              Goods_size: item.规格,
              Goods_in_state: item.采购状态,
              Goods_sub: item.编码,
              Goods_name: item.名称,
            };
          });
          $parent.$refs.detail.rowData.unshift(...arr);
        } catch (error) {
          console.error(error);
        }
      });
      this.onModelClose();
      return (this.model = false);
    },
  },
};
</script>
    
    <style>
</style>